/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ReplyToMessage.java
 *
 * Created on 25/04/2010, 19:28:12
 */

package forum.client.ui;

import forum.client.controllerlayer.ControllerHandler;
import forum.client.controllerlayer.ControllerHandlerFactory;
import forum.client.ui.events.ForumTreeErrorEvent;
import java.awt.Component;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.SwingWorker;

/**
 *
 * @author furmanal
 */
public class ReplyToMessage extends javax.swing.JFrame {

	private static final long serialVersionUID = -7592899811501470828L;
	Component fatherComp;
    private ExecutorService m_pool = Executors.newCachedThreadPool();
    private ControllerHandler m_pipe;
    Component myself;
    int replyToId;
    JButton buttonToEnable;

    /** Creates new form ReplyToMessage */
    public ReplyToMessage(int replyId) {
        m_pipe = ControllerHandlerFactory.getPipe();
        initComponents();
        replyToId = replyId;
        myself = this;
        this.setTitle("BannanaForum- Add reply");
        this.setLocationRelativeTo(null);
        this.setResizable(false);
        
        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                cancelButtonActionPerformed(null);
            }
        });
    }
    /** Creates new form ReplyToMessage */
    public ReplyToMessage(int replyId,JButton buttonToEnable) {
        m_pipe = ControllerHandlerFactory.getPipe();
        initComponents();
        replyToId = replyId;
        myself = this;
        this.setTitle("BannanaForum- Add reply");
        this.setLocationRelativeTo(null);
        this.setResizable(false);

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                cancelButtonActionPerformed(null);
            }
        });
        this.buttonToEnable=buttonToEnable;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        addMessageButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        contentLabel = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        contentTextArea = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        addMessageButton.setIcon(new javax.swing.ImageIcon(".\\images\\ok.jpg")); // NOI18N
        addMessageButton.setText("Add Message");
        addMessageButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addMessageButtonActionPerformed(evt);
            }
        });

        cancelButton.setIcon(new javax.swing.ImageIcon(".\\images\\process-stop.png")); // NOI18N
        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        contentLabel.setText("Content:");

        contentTextArea.setColumns(20);
        contentTextArea.setRows(5);
        jScrollPane1.setViewportView(contentTextArea);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(contentLabel)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(65, 65, 65)
                        .addComponent(addMessageButton)
                        .addGap(30, 30, 30)
                        .addComponent(cancelButton))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 361, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(53, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(40, 40, 40)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 188, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(39, 39, 39)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cancelButton)
                            .addComponent(addMessageButton)))
                    .addComponent(contentLabel))
                .addContainerGap(52, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void addMessageButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addMessageButtonActionPerformed
        if (contentTextArea.getText().length()==0) {
            JOptionPane.showMessageDialog(this,
			    "Cannot reply with an empty message.",
			    "Operation failed.",
			    JOptionPane.WARNING_MESSAGE);
            return;
        }
        SwingWorker<String[], Void> replyWorker = new SwingWorker<String[], Void>() {
               public String[] doInBackground() {
		  //m_pipe.addReplyToMessage(replyToId, jTextArea1.getText(), myself);
                    return m_pipe.addReplyToMessage(replyToId, contentTextArea.getText(), buttonToEnable);
		}
		};
        m_pool.execute(replyWorker);
        try {
            String[] ans = (String[])replyWorker.get();
            if (ans[0].equals("succ")){
                m_pipe.refreshForum(m_pool);
            }
            else{
                m_pipe.notifyObservers(new ForumTreeErrorEvent(ans[1]));
            }
        } catch (InterruptedException ex) {}
        catch (ExecutionException ex) {
            m_pipe.notifyObservers(new ForumTreeErrorEvent("Execution exception."));
        }
        this.setVisible(false);
        //this.fatherComp.setVisible(true);
}//GEN-LAST:event_addMessageButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        this.setVisible(false);
        this.buttonToEnable.setEnabled(true);
       // this.fatherComp.setVisible(true);
}//GEN-LAST:event_cancelButtonActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addMessageButton;
    private javax.swing.JButton cancelButton;
    private javax.swing.JLabel contentLabel;
    private javax.swing.JTextArea contentTextArea;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

}
